package org.itboys.core.service.mongo.web;

import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.entity.mongo.SundayProductTag;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Auther: 罗尧坤
 * Date: 2018/8/24
 * Description:商品+标签链接服务类
 */
@Service
public class SundayProductTagService extends BaseMongoService<SundayProductTag> {
    @Resource(name = "mobileDs")
    private MongoDataSource mongoDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mongoDataSource;
    }
    @Override
    protected Class<SundayProductTag> getEntityClass() {
        return SundayProductTag.class;
    }

    /**
     * 分页查询
     * @param request
     * @return
     */
    public PageResult<SundayProductTag> selectTags(HttpServletRequest request, Long productId){
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request);
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("productId", productId);

        return super.pageQuery(request, param);
    }

    /**
     * 查询单挑记录
     * @param id
     * @return
     */
    public SundayProductTag findOne(Long id){
        if (LongUtil.isNotZreo(id)){
            return super.getById(id);
        }else {
            return new SundayProductTag();
        }
    }

    /**
     * 新增或者保存
     * @param id
     * @param tag
     */
    public void save(Long id, SundayProductTag tag){
        if (LongUtil.isNotZreo(id)){
            super.updateExceptEmpty(id, tag);
        }else {
            super.save(tag);
        }
    }

}
